package lib.modules;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import flexjson.JSONSerializer;
import lib.base.EgoHoliday;
import lib.base.EgoModule;
import lib.data.Department;
import lib.data.Employee;

public class ModLeaveRequests extends EgoModule
{
	public ModLeaveRequests(HttpServletRequest req)
	{
		super(req);
		req.getSession().setAttribute("template", "modules/leave_requests/index.jsp");
	}
	
	@Override
	public void work() throws Exception
	{
		if (_request.getParameter("action") != null)
		{
			if (_request.getParameter("action").equals( "read" ))
			{
				_read();
			} else if (_request.getParameter("action").equals( "del" ))
			{
				_do();
			}
		}
	}
	
	private void _read() throws Exception
	{
		Map<String,Object> data = new HashMap<String,Object>();
		
		data.put("identifier", "id");
		data.put("label", "start");
		
		ArrayList<Map<String,Object>> items = new ArrayList<Map<String,Object>>();

		// for current and next year
		Calendar cal = Calendar.getInstance();
		int cur_year = cal.get(Calendar.YEAR);
		for (int iy=0; iy<2; iy++)
		{
			cur_year += iy;
			
			// go through departments
			ArrayList<Department> deps = _curEmp.getDepartments("supervisor");
			for (int i=0; i<deps.size(); i++)
			{
				ArrayList<EgoHoliday> x = _curCust.holidayManager.getHolidays(
					deps.get(i)
					, EgoHoliday.STATUS_REQUESTED
					, String.valueOf(cur_year)
				);
				for (int ii=0; ii<x.size(); ii++)
				{
					// cannot manage own leaves
					if (!_curEmp.getField("manageOwnLeaves").toString().equals("true") && x.get(ii).getEmployee().getField("employeeID").equals( _curEmp.getField("employeeID") ))
					{
						continue;
					}

					Map<String, Object> attr = new HashMap<String, Object>();
					attr.put("id", String.valueOf(iy)+String.valueOf(i)+String.valueOf(ii));
					attr.put(
						"name"
						, x.get(ii).getEmployee().getField("givenName").toString()
							+ " " + x.get(ii).getEmployee().getField("lastName").toString()
					);
					attr.put(
						"account"
						, x.get(ii).getEmployee().getField("account").toString()
					);
					attr.put("start", x.get(ii).getStartStamp());
					attr.put("end", x.get(ii).getEndStamp());
					attr.put("type", x.get(ii).getType());
					attr.put("length", x.get(ii).getLength());
					
					// already added?
					// avoid duplicated entries
					boolean added = false;
					for (int z=0; z<items.size(); z++)
					{
						if (
							items.get(z).get("account").equals( attr.get("account") )
							&& items.get(z).get("start").equals( attr.get("start") )
						) {
							added = true;
						}
					}
					if (!added)
					{
						items.add(attr);
					}
				}
			}
		}

		data.put("items", items);

		_request.setAttribute("ajax_return", new JSONSerializer().serialize(data));
		_request.getSession().setAttribute("template", "ajax_return.jsp");
	}
	
	private void _do() throws Exception
	{
		// get the employee instance of the holiday
		Employee emp = _curCust.getEmployeeByMail(_request.getParameter("acc"));
		EgoHoliday holiday = _curCust.holidayManager.getHoliday(
			emp
			, _request.getParameter("start")
			, _request.getParameter("end")
		);
		
		if (_request.getParameter("opt").equals( "accept" ))
		{
			String opt;
			opt = EgoHoliday.STATUS_ACCEPTED;
			_curCust.holidayManager.updateHoliday(
				emp
				, holiday
				, opt
			);
		} else
		{
			// deny
			String opt;
			opt = EgoHoliday.STATUS_DENIED;
			_curCust.holidayManager.updateHoliday(
				emp
				, holiday
				, opt
			);
		}
		_request.getSession().setAttribute("template", "ajax_return.jsp");
	}
}
